package fp.thetrainline.utils;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;

public class DriverSupport {

	static DesiredCapabilities capabilities = DesiredCapabilities.chrome();
	
	private static DriverSupport seleniumSupport;

	private WebDriver driver;

	private DriverSupport( WebDriver driver ) {
		this.driver = driver;
	}

	public static DriverSupport getInstance( WebDriver driver ) {
		if( seleniumSupport == null )
			seleniumSupport = new DriverSupport( driver );

		return seleniumSupport;
	}

	public WebElement findElement( final ElementType elType, final String id ) {
		return elType.findElement( driver, id );
	}

	public void click( ElementType elType, String id ) {
		findElement( elType, id ).click();
		waitForIt();
	}

	public void write( ElementType elType, String id, String text ) {
		findElement( elType, id ).sendKeys( text );
		waitForIt();
	}

	public void selectValue( ElementType elType, String id, String value ) {
		final Select selectBox = new Select( findElement( elType, id ) );
		selectBox.selectByValue( value );
	}
	
	public void waitForIt() {
		try {
			Thread.sleep( 3500 );
		} catch( InterruptedException e ) {
			e.printStackTrace();
		}
	}

	public void openPage( String url ) {
		driver.navigate().to( url );
	}

	public void openTrainline() {
		openPage( ElementName.THE_TRAINLINE_PAGE );
	}

	public void quit() {
		driver.close();
	}

}
